<template>
  <div>
    <el-row :gutter="10" style="margin-bottom: 60px">
      <el-col :span="6">
        <el-card style="color: #409eff">
          <div><i class="el-icon-user-solid" />交警用户总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
            {{ total }}
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color: #f56c6c">
          <div><i class="el-icon-money" />海淀区案件数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
            99
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color: #67c23a">
          <div><i class="el-icon-bank-card" />朝阳区案件数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
            89
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card style="color: #409eff">
          <div style="color: #409eff"><i class="el-icon-s-shop" />案件总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
            189
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div id="main" style="width: 500px; height: 400px"></div>
      </el-col>
      <el-col :span="12">
        <div id="pie" style="width: 500px; height: 400px"></div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  name: "Home",
  data() {
    return {
      total: 0, // 初始值
    };
  },
  mounted() {
    //使用mounted的目的是为了等页面元素渲染之后再触发

    var chartDom = document.getElementById("main");
    var myChart = echarts.init(chartDom);
    var charOption;
    charOption = {
      title: {
        text: "各季度交警用户统计",
        subtext: "趋势图",
        left: "center",
      },
      xAxis: {
        type: "category",
        data: ["第一季度", "第二季度", "第三季度", "第四季度"],
      },
      yAxis: {
        type: "value",
      },
      series: [
        {
          data: [],
          type: "line",
        },
        {
          data: [],
          type: "bar",
        },
      ],
    };
    var pieDom = document.getElementById("pie");
    var pieChart = echarts.init(pieDom);
    var pieOption;

    pieOption = {
      title: {
        text: "各季度交警用户统计",
        subtext: "比例图",
        left: "center",
      },
      tooltip: {
        trigger: "item",
      },
      legend: {
        orient: "vertical",
        left: "left",
      },
      series: [
        {
          type: "pie",
          radius: "70%",
          label: {
            normal: {
              show: true,
              position: "inner",
              textStyle: {
                fontWeight: 300,
                fontSize: 16,
                color: "#fff",
              },
              formatter: "{d}%", //自定义显示格式(b:name, c:value, d:百分比)
            },
          },
          data: [],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: "rgba(0, 0, 0, 0.5)",
            },
          },
        },
      ],
    };
    this.request("http://localhost:8084/echarts/members").then((res) => {
      charOption.series[0].data = res.data;
      charOption.series[1].data = res.data;
      myChart.setOption(charOption);
      console.log(res.data[0]);
      this.total = res.data[0] + res.data[1] + res.data[2] + res.data[3];
      console.log(this.total);
      pieOption.series[0].data = [
        { name: "第一季度", value: res.data[0] },
        { name: "第二季度", value: res.data[1] },
        { name: "第三季度", value: res.data[2] },
        { name: "第四季度", value: res.data[3] },
      ];
      pieChart.setOption(pieOption);
    });
  },
};
</script>
<style scoped>
/* 在 Vue.js 中，<style scoped> 是一个特殊的语法，用于限制样式的作用范围，确保样式仅对当前组件生效，而不会影响到其他组件或全局样式。 */
/* 当你在 Vue 组件中使用 <style scoped> 时，Vue 会自动为该组件的 HTML 元素添加一个唯一的属性（如 data-v-f3f3eg9），并在 CSS 选择器中添加这个属性，以确保样式只应用到该组件的根元素及其子元素上。 */
</style>
